package cn.net.xyan.easy.gwt.client.ui;

import cn.net.xyan.easy.gwt.client.EZGwt;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.Response;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.impl.RequestCallbackAdapter;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import org.gwtbootstrap3.client.ui.NavbarBrand;
import org.gwtbootstrap3.extras.bootbox.client.Bootbox;
import org.gwtbootstrap3.extras.bootbox.client.callback.ConfirmCallback;

/**
 * Created by zarra on 16/2/18.
 */
public class Navbar extends Composite {
    interface NavbarBinder extends UiBinder<Widget, Navbar> {
    }

    private static NavbarBinder binder = GWT.create(NavbarBinder.class);

    public void setTitle(String title) {
        this.title.setText(title);
    }

    public String getTitle() {
        return this.title.getText();
    }

    @UiField
    NavbarBrand title;

    public Navbar() {
        initWidget(binder.createAndBindUi(this));
    }

    @UiHandler("changePassword")
    void changePassword(ClickEvent event){

    }

    @UiHandler("logout")
    void logout(ClickEvent event) {

        Bootbox.confirm("Do you want to logout", new ConfirmCallback() {
            @Override
            public void callback(boolean result) {
                if (result) {
                    EZGwt.logout(new RequestCallback() {

                        @Override
                        public void onResponseReceived(Request request, Response response) {
                            String newURL = Window.Location.createUrlBuilder().buildString();
                            Window.Location.replace(newURL);
                        }

                        @Override
                        public void onError(Request request, Throwable exception) {

                        }
                    });
                }
            }
        });


    }
}
